package com.info33.platform.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.info33.platform.common.constant.TableConstant;
import com.info33.platform.common.mvc.controller.BaseController;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.common.validated.SaveValidated;
import com.info33.platform.common.validated.UpdateValidated;
import com.info33.platform.system.dto.SysDictionaryDTO;
import com.info33.platform.system.entity.SysDictionary;
import com.info33.platform.system.service.SysDictionaryService;
import com.info33.platform.system.vo.SysDictionaryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author admin
 */
@RestController
@RequestMapping("/SysDictionary")
@Api(tags = "系统 - 字典")
public class SysDictionaryController extends BaseController<SysDictionaryService, SysDictionary> {

    private final SysDictionaryService sysDictionaryService;

    @Autowired
    public SysDictionaryController(SysDictionaryService sysDictionaryService) {
        super(sysDictionaryService);
        this.sysDictionaryService = sysDictionaryService;
    }

    @GetMapping(value = "/pc/init/{indexes}")
    @ApiOperation(value = "pc - 初始化查询")
    public Result<SysDictionary> pcInit(
            @PathVariable(value = "indexes") String indexes) {
        return Result.ok(sysDictionaryService.getOne(new QueryWrapper<SysDictionary>()
                .eq("indexes", indexes)
                .last(TableConstant.LIMIT_ONE)));
    }

    @PostMapping(value = "/public/indexes/{pageNum}/{pageSize}")
    @ApiOperation(value = "public - 索引查询 - 分页", notes = "有索引就用此接口<br/>" +
            "传入 -1，-1 查询全部")
    public Result<IPage<SysDictionaryVO>> appIndexesPage(
            @RequestBody SysDictionaryDTO dto,
            @PathVariable int pageNum,
            @PathVariable int pageSize) {
        return sysDictionaryService.appIndexesPage(new Page<>(pageNum, pageSize), dto, getCurrentUser());
    }

    @PostMapping(value = "/public/indexes")
    @ApiOperation(value = "public - 索引查询", notes = "有索引就用此接口")
    public Result<List<SysDictionaryVO>> appIndexes(
            @RequestBody SysDictionaryDTO dto) {
        return Result.ok(sysDictionaryService.appIndexes(dto, getCurrentUser()));
    }

    @PostMapping(value = "/public/pid/{pageNum}/{pageSize}")
    @ApiOperation(value = "public - pid 查询 - 分页", notes = "传入 -1，-1 查询全部")
    public Result<IPage<SysDictionaryVO>> appPidPage(
            @RequestBody SysDictionaryDTO dto,
            @PathVariable int pageNum,
            @PathVariable int pageSize) {
        return Result.ok(sysDictionaryService.appPid(new Page<>(pageNum, pageSize), dto, getCurrentUser()));
    }

    @PostMapping(value = "/public/appoint")
    @ApiOperation(value = "public - pid 查询")
    public Result<List<SysDictionaryVO>> appPid(
            @RequestBody SysDictionaryDTO dto) {
        return Result.ok(sysDictionaryService.appPid(dto, getCurrentUser()));
    }

    @PostMapping(value = "/public/list/indexes")
    @ApiOperation(value = "app - 返回多个索引的集合")
    public Result<Map<String, List<SysDictionaryVO>>> publicIndexesList(
            @RequestBody SysDictionaryDTO dto) {
        return Result.ok(sysDictionaryService.publicIndexesList(dto, getCurrentUser()));
    }

    @PostMapping(value = "/gw/list/indexes")
    @ApiOperation(value = "门户 - 返回多个索引的集合")
    public Result<Map<String, List<SysDictionaryVO>>> publicGetIndexesList(
            @RequestBody SysDictionaryDTO dto) {
        return Result.ok(sysDictionaryService.publicGetIndexesList(dto));
    }

    @GetMapping(value = "/pc/list/{ids}")
    @ApiOperation(value = "pc - 多字典查询", notes = "id 集合")
    public Result<List<SysDictionary>> pcList(
            @PathVariable(value = "ids") String ids) {
        return Result.ok(sysDictionaryService.list(new QueryWrapper<SysDictionary>().apply("FIND_IN_SET(id, {0}) > 0", ids)));
    }

    @GetMapping(value = "/tree/{indexes}")
    @ApiOperation(value = "pc - 某索引下所有字典 - 新", notes = "所有兄弟列表")
    public Result<List<SysDictionaryVO>> pcTreeListByCode(
            @PathVariable String indexes) {
        return Result.ok(sysDictionaryService.pcTreeListByCode(indexes, getCurrentUser()));
    }

    @PostMapping(value = "/public/brother")
    @ApiOperation(value = "同级兄弟查询", notes = "根据 id 或 ids")
    public Result<List<SysDictionaryVO>> publicBrother(
            @RequestBody SysDictionaryDTO dto) {
        return Result.ok(sysDictionaryService.publicBrother(dto, getCurrentUser()));
    }

    @GetMapping(value = "/pc/tree/{pid}")
    @ApiOperation(value = "pc - 树结构查询")
    public Result<List<SysDictionaryVO>> pcTreeList(
            @PathVariable(value = "pid") Long pid) {
        return Result.ok(sysDictionaryService.pcTree(pid));
    }

    @PostMapping(value = "/public/tree/structure")
    @ApiOperation(value = "pc - 树结构数据", notes = "可根据 pid，可根据 indexes<br/>" +
            "公开 api 根据 sysTenantDTO.url 租户域名<br/>" +
            "返回子集树结构数据")
    public Result<List<SysDictionaryVO>> treeStructure(
            @RequestBody SysDictionaryDTO dto) {
        return Result.ok(sysDictionaryService.treeStructure(dto, getCurrentUser()));
    }

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * PC 端
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    @PostMapping(value = "/pc/back/{pageNum}/{pageSize}")
    @ApiOperation(value = "pc - 后退")
    public Result<IPage<SysDictionaryVO>> pcBack(
            @RequestBody SysDictionaryDTO dto,
            @PathVariable int pageNum,
            @PathVariable int pageSize) {
        dto.setTenantId(getTenantId());
        return Result.ok(sysDictionaryService.pcBack(new Page<>(pageNum, pageSize), dto));
    }

    @PutMapping(value = "/pc/move/{id}")
    @ApiOperation(value = "pc - 逻辑删除")
    public Result<Object> pcMove(
            @PathVariable(value = "id") Long id) {
        return sysDictionaryService.pcMove(id, getCurrentUser());
    }

    @PutMapping(value = "/pc/edit")
    @ApiOperation(value = "pc - 编辑")
    public Result<Object> pcEdit(
            @Validated(value = {UpdateValidated.class}) @RequestBody SysDictionaryDTO dto) {
        return sysDictionaryService.pcEdit(dto, getCurrentUser());
    }

    @PostMapping(value = "/pc/add")
    @ApiOperation(value = "pc - 新增")
    public Result<Object> pcAdd(
            @Validated(value = {SaveValidated.class}) @RequestBody SysDictionary dto) {
        return sysDictionaryService.pcAdd(dto, getCurrentUser());
    }

}